import axios from '@/utils/request'
const ORDER_STATUS = {
  '-2': '支付失败',
  '-1': '已取消',
  1: '待支付',
  2: '待核销',
  3: '已核销',
  4: '已结算',
  5: '已完成'
}
const STATUS_COLOR = {
  '-2': 'danger',
  '-1': 'info',
  1: 'primary',
  2: 'warning',
  3: 'warning',
  4: 'success',
  5: 'success'
}

export default {
  Order: {
    form: {
      fields: [
        'store',
        'counter',
        {
          property: 'status',
          type: 'select',
          type_options: {
            options: [
              { value: '-2', label: '支付失败' },
              { value: '-1', label: '已取消' },
              { value: '1', label: '已提交' },
              { value: '2', label: '已成交' },
              { value: '3', label: '已发货' },
              { value: '4', label: '已结算' },
              { value: '5', label: '已完成' }
            ]
          }
        },
        {
          property: 'staff',
          field_options: { label: '业务员' },
          required: false
        },
        {
          property: 'booking',
          field_options: { label: '订台人' },
          required: false
        },
        {
          property: 'leader',
          field_options: { label: '队长' },
          required: false
        },
        {
          property: 'broker',
          field_options: { label: '经纪人' },
          required: false
        }
      ]
    },
    list: {
      // export: {
      //   query: {
      //     '@display': `{
      //       'id' : 'entity.getId()',
      //       'invoice' : 'entity.getInvoice().__toString()',
      //       'invoiceAmount' : 'entity.getInvoice().getAmount()',
      //       'createdTime' : 'entity.getCreatedTime().format("Y-m-d H:i:s")',
      //       'paidTime' : 'entity.getInvoice().getPaidTime().format("Y-m-d H:i:s")',
      //       'store' : 'entity.getStore().__toString()',
      //       'items' : 'ArrayCommon.reduce(entity.getItems().toArray(), "carry~item.__toString()~hyphen", null, { hyphen : "; "} )',
      //       'status' : '${JSON.stringify(ORDER_STATUS)}[entity.getStatus()]',
      //       'totalPrice' : 'entity.getTotalPrice()',
      //       'price' : 'entity.getPrice()',
      //     }`,
      //     limit: 10000000
      //   },
      //   label: {
      //     'id': 'id',
      //     'invoice': '支付记录',
      //     'invoiceAmount': '收款总金额',
      //     'createdTime': '创建时间',
      //     'paidTime': '支付时间',
      //     'store': '店铺',
      //     'items': '产品',
      //     'status': '状态',
      //     'totalPrice': '总价',
      //     'price': '实收'
      //   }
      // },
      list_filter: {
        status: Object.assign({ __label: '状态' }, ORDER_STATUS),
        startTime: {
          expression: `
          entity.getCreatedTime() >= datetime.get(":value")
        `,
          label: '开始时间',
          type: 'datetime'
        },
        endTime: {
          expression: `
          entity.getCreatedTime() < datetime.get(":value")
        `,
          label: '结束时间',
          type: 'datetime'
        }
      },
      disabled_actions: ['new', 'delete', 'edit'],
      actions: [
        {
          name: 'refund',
          position: 'list',
          component: {
            props: ['record', 'refresh'],
            data() {
              return {
                loading: false
              }
            },
            render(h) {
              console.log(123,this.record)
              if (parseInt(this.record.invoice.__metadata.status) === 1 && parseInt(this.record.status) === 2) {
                return (
                  <el-popconfirm title='确定退款当前订单？'
                    {...{
                      on: {
                        onConfirm: (val) => {
                          this.loading = true
                          axios.post(`/manage/orders/${this.record.id}/cancel-and-refund`).then((res) => {
                            this.$message.success('执行成功！')
                            this.refresh()
                          }).finally(() => {
                            this.loading = false
                          })
                        }
                      }
                    }}>
                    <el-button slot='reference' size='small' type='danger' icon='el-icon-back' loading={this.loading} plain>退款</el-button>
                  </el-popconfirm>
                )
              } else if (parseInt(this.record.status) >= 5) {
                return (
                  <el-popconfirm title='确定回滚当前订单的所有分账金额？'
                    {...{
                      on: {
                        onConfirm: (val) => {
                          this.loading = true
                          axios.post(`/manage/orders/${this.record.id}/settle-reverse`).then((res) => {
                            axios.post(`/manage/orders/${this.record.id}/settle-remove`).then((res) => {
                              this.$message.success('执行成功！')
                              this.refresh()
                            })
                          }).finally(() => {
                            this.loading = false
                          })
                        }
                      }
                    }}>
                    <el-button slot='reference' size='small' type='danger' icon='el-icon-refresh-left' loading={this.loading} plain>回滚分账</el-button>
                  </el-popconfirm>
                )
              } else if (parseInt(this.record.status) === 1) {
                return (
                  <el-popconfirm title='是否确定取消该订单？'
                    {...{
                      on: {
                        onConfirm: (val) => {
                          this.loading = true
                          axios.put(`/manage/orders/${this.record.id}`, { status: -1 }).then((res) => {
                            this.$message.success('执行成功！')
                            this.refresh()
                          }).finally(() => {
                            this.loading = false
                          })
                        }
                      }
                    }}>
                    <el-button slot='reference' size='small' type='info' icon='el-icon-delete' loading={this.loading} plain>取消订单</el-button>
                  </el-popconfirm>
                )
              }
            }
          }
        },
        {
          name: 'button',
          position: 'list',
          component: {
            props: ['record', 'refresh'],
            data() {
              return {
                loading: false
              }
            },
            render(h) {
              if (parseInt(this.record.status) === 4) {
                return (
                  <div>
                    <el-popconfirm title='确定反结算当前订单？'
                      {...{
                        on: {
                          onConfirm: (val) => {
                            this.loading = true
                            axios.post(`/manage/orders/${this.record.id}/settle-remove`).then((res) => {
                              this.$message.success('执行成功！')
                              this.refresh()
                            }).finally(() => {
                              this.loading = false
                            })
                          }
                        }
                      }} style={{ marginLeft: '3px' }}>
                      <el-button slot='reference' size='small' type='warning' icon='el-icon-delete' loading={this.loading} plain>反结算</el-button>
                    </el-popconfirm>
                    <el-popconfirm title='确定对当前订单执行分账？'
                      {...{
                        on: {
                          onConfirm: (val) => {
                            this.loading = true
                            axios.post(`/manage/orders/${this.record.id}/settle-execute`).then((res) => {
                              this.$message.success('执行成功！')
                              this.refresh()
                            }).finally(() => {
                              this.loading = false
                            })
                          }
                        }
                      }} style={{ marginLeft: '3px' }}>
                      <el-button slot='reference' size='small' type='primary' icon='el-icon-delete' loading={this.loading} plain>执行分账</el-button>
                    </el-popconfirm>
                  </div>
                )
              } else if (this.record.laterPay === false && parseInt(this.record.status) >= 2 && parseInt(this.record.status) <= 3) {
                return (
                  <el-popconfirm title='确定结算当前订单？'
                    {...{
                      on: {
                        onConfirm: (val) => {
                          this.loading = true
                          axios.post(`/manage/orders/${this.record.id}/settle-generate`).then((res) => {
                            this.$message.success('执行成功！')
                            this.refresh()
                          }).finally(() => {
                            this.loading = false
                          })
                        }
                      }
                    }} style={{ marginLeft: '3px' }}>
                    <el-button slot='reference' size='small' type='success' icon='el-icon-delete' loading={this.loading} plain>结算</el-button>
                  </el-popconfirm>
                )
              }
            }
          }
        }
      ],
      query: {
        '@filter': 'entity.getStatus() != 0',
        '@order': 'entity.id|DESC',
        '@expands': "['entity.items.specification','entity.invoice']"
      },
      list_display: [
        'createdTime',
        'user',
        {
          property: 'verifier',
          label: '核销账号'
        },
        {
          property: 'items',
          label: '购买列表',
          component: {
            props: ['data'],
            render(h) {
              return (
                <div>
                  {
                    this.data.map(v => {
                      return (
                        <div style='padding-top:5px;'>
                          <el-tag type='primary' effect='plain'>
                            {v.__metadata.specification.__metadata.product.__toString} X {v.__metadata.quantity}{v.__metadata.specification.__metadata.name}
                          </el-tag>
                        </div>
                      )
                    })
                  }
                </div>
              )
            }
          }
        },
        'price',
        'comment',
        {
          property: 'status',
          label: '订单状态',
          component: {
            props: ['data'],
            data() {
              return {
                status: ORDER_STATUS,
                color: STATUS_COLOR,
                show: false,
                settleItems: []
              }
            },
            render(h) {
              return <div>
                <el-tag type={this.color[this.data]} effect='plain'>{this.status[this.data]}</el-tag>
              </div>
            }
          }
        }
      ]
    }
  }
}
